package com.haven.webrobotserver.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.haven.webrobotserver.domain.Testdata;
import com.haven.webrobotserver.service.TestdataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin("http://localhost:8080")
@RequestMapping("/testdata")
public class TestdataController {

    @Autowired(required = false)
    private TestdataService testdataService;

    @GetMapping("/getTestdataList")
    public JSONObject getTestdataList(@RequestParam(name = "testcaseId") int testcaseId) {
        JSONObject result = new JSONObject();

        try {
            List<Testdata> list = testdataService.getTestdataListByTestcaseid(testcaseId);
            if (list != null) {
                String s = JSONObject.toJSONString(list);
                JSONArray objects = JSONObject.parseArray(s);
                result.put("data", objects);
                result.put("status", 200);
                result.put("msg", "success");
            } else {
                result.put("status", 500);
                result.put("msg", "fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", 500);
            result.put("msg", "fail");
        }

        return result;
    }

    @PostMapping("/updateTestdata")
    public JSONObject updateTestdata(@RequestBody Map<String, String> params) {
        JSONObject result = new JSONObject();

        /*封装testdata*/
        Testdata testdata = new Testdata();
        testdata.setId(Integer.parseInt(params.get("id")));
        testdata.setName(params.get("name"));
        testdata.setDescription(params.get("description"));

        try {
            testdataService.updateTestdata(testdata);
            result.put("status", 200);
            result.put("msg", "success");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", 500);
            result.put("msg", "fail");
        }

        return result;
    }

    @PostMapping("/newTestdata")
    public JSONObject newTestdata(@RequestBody Map<String, String> params) {
        JSONObject result = new JSONObject();

        /*封装testdata*/
        Testdata testdata = new Testdata();
        testdata.setName(params.get("name"));
        testdata.setTestcaseId(Integer.parseInt(params.get("testcaseId")));
        testdata.setDescription(params.get("description"));

        try {
            testdataService.newTestdata(testdata);

            /*将新插入的testdata返回出去*/

            String s = JSONObject.toJSONString(testdata);
            JSONObject jsonObject = JSONObject.parseObject(s);

            result.put("status", 200);
            result.put("msg", "success");
            result.put("data",jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", 500);
            result.put("msg", "fail");
        }

        return result;
    }

    @GetMapping("/deleteOne")
    public JSONObject deleteOne(@RequestParam(name="id") int id){
        JSONObject result = new JSONObject();

        try{
            testdataService.deleteOne(id);
            result.put("status",200);
            result.put("msg","success");
        }catch(Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","fail");
        }

        return result;
    }
}